package datas

import (
	"os"
	"time"

	"github.com/gookit/color"

	"gin-bms/global"
	"gin-bms/model"

	"gorm.io/gorm"
)

func InitSysDictionaryDetail(db *gorm.DB) {
	status := new(bool)
	*status = true
	DictionaryDetail := []model.SysDictionaryDetail{
		{global.BASE_MODEL{ID: 1, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "tinyint", 0, status, 0, "1字节整型（bool类型）", 2},
		{global.BASE_MODEL{ID: 2, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "smallint", 1, status, 1, "2字节整型", 2},
		{global.BASE_MODEL{ID: 3, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "mediumint", 2, status, 2, "3字节整型", 2},
		{global.BASE_MODEL{ID: 4, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "int", 3, status, 3, "4字节整型", 2},
		{global.BASE_MODEL{ID: 5, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "bigint", 4, status, 4, "8字节整型", 2},
		{global.BASE_MODEL{ID: 6, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "date", 0, status, 0, "YYYY-MM-DD", 3},
		{global.BASE_MODEL{ID: 7, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "time", 1, status, 1, "HH:MM:SS", 3},
		{global.BASE_MODEL{ID: 8, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "year", 2, status, 2, "YYYY", 3},
		{global.BASE_MODEL{ID: 9, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "datetime", 3, status, 3, "YYYY-MM-DD HH:MM:SS", 3},
		{global.BASE_MODEL{ID: 10, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "timestamp", 4, status, 4, " YYYY-MM-DD HH:MM:SS可作时间戳", 3},
		{global.BASE_MODEL{ID: 11, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "float", 0, status, 0, "单精度浮点型", 4},
		{global.BASE_MODEL{ID: 12, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "double", 1, status, 1, "双精度浮点型", 4},
		{global.BASE_MODEL{ID: 13, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "decimal", 2, status, 2, "定点型", 4},
		{global.BASE_MODEL{ID: 14, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "char", 0, status, 0, "固定长度字符串", 5},
		{global.BASE_MODEL{ID: 15, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "varchar", 1, status, 1, "可变长度字符串", 5},
		{global.BASE_MODEL{ID: 16, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "tinyblob", 2, status, 2, "不超过255个字符的二进制字符串", 5},
		{global.BASE_MODEL{ID: 17, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "tinytext", 3, status, 3, "短文本字符串", 5},
		{global.BASE_MODEL{ID: 18, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "blob", 4, status, 4, "二进制形式的长文本数据", 5},
		{global.BASE_MODEL{ID: 19, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "text", 5, status, 5, "长文本数据", 5},
		{global.BASE_MODEL{ID: 20, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "mediumblob", 6, status, 6, "二进制形式的中等长度文本数据", 5},
		{global.BASE_MODEL{ID: 21, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "mediumtext", 7, status, 7, "中等长度文本数据", 5},
		{global.BASE_MODEL{ID: 22, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "longblob", 8, status, 8, "二进制形式的极大文本数据", 5},
		{global.BASE_MODEL{ID: 23, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "longtext", 9, status, 9, "极大文本数据", 5},
		{global.BASE_MODEL{ID: 24, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "tinyint", 0, status, 0, "bool类型（1字节整型）", 6},
	}
	if err := db.Transaction(func(tx *gorm.DB) error {
		if tx.Where("id IN ?", []int{1, 24}).Find(&[]model.SysDictionaryDetail{}).RowsAffected == 2 {
			color.Danger.Println("sys_dictionary_details表的初始数据已存在!")
			return nil
		}
		if err := tx.Create(&DictionaryDetail).Error; err != nil { // 遇到错误时回滚事务
			return err
		}
		return nil
	}); err != nil {
		color.Warn.Printf("[Mysql]--> sys_dictionary_details 表的初始数据失败,err: %v\n", err)
		os.Exit(0)
	}
}
